package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Server {
    private ServerSocket serverSocket;
    Socket socket;

    public Server() {
        for(int i=0;i<2;i++) {
            try {
                System.out.println("开始申请端口");
                serverSocket = new ServerSocket(9998);
                System.out.println("申请端口成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
   }
//
    public void start() {
        try {
//            //Client
//            while(true) {
                System.out.println("等待与客户端连接");
                socket = serverSocket.accept();
                System.out.println("与客户端连接成功");
//
//                InputStream in = socket.getInputStream();
//                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
//                BufferedReader br = new BufferedReader(isr);
//
//                String line;
//                while ((line = br.readLine())!=null) {
//                    System.out.println(line);
//                }
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
//
    public static void main(String[] args) {
//        Runnable r1 = new Phone1();
//        Runnable r2 = new Phone2();
//
//        Thread t1 = new Thread(r1);
//        Thread t2 = new Thread(r2);

        Runnable r3 = new Contact();
        Thread t3 = new Thread(r3);

//        t1.start();
//        t2.start();
        t3.start();

    }
}

//第一台手机
class Phone1 extends Server implements Runnable{
//    ServerSocket serverSocket;

    public Phone1() {
//        try {
//            serverSocket = new ServerSocket(9999);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public void run() {
//        try {
//            Socket socket = serverSocket.accept();
//
//            InputStream is = socket.getInputStream();
//            InputStreamReader isr = new InputStreamReader(is,StandardCharsets.UTF_8);
//            BufferedReader br = new BufferedReader(isr);
//
//            String line;
//            while((line=br.readLine())!=null) {
//                System.out.println(line);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}

//第二台手机
class Phone2 extends Server implements Runnable {
//    private ServerSocket serverSocket;

    public Phone2() {
//        try {
//            serverSocket = new ServerSocket(9998);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
    public void run() {
//        try {
//            Socket socket = serverSocket.accept();
//
            //输入流
//            InputStream is = socket.getInputStream();
//            InputStreamReader isr = new InputStreamReader(is,StandardCharsets.UTF_8);
//            BufferedReader br = new BufferedReader(isr);
//
//            String line;
//            while((line=br.readLine())!=null) {
//                System.out.println(line);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

}

class Contact extends Server implements Runnable{
    private ServerSocket serverSocket;

    public Contact() {
//        try {
//            serverSocket = new ServerSocket(9888);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public void run() {
        try {
//            Socket socket = serverSocket.accept();

            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is,StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isr);

            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os,StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);

            String line;
            while((line=br.readLine())!=null) {
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
